package org.webshop.frontend.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.LinkedList;

import org.webshop.backend.dbahif.DBAccess;
import org.webshop.backend.data.Product;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class OrderServlet
 */
@WebServlet("/order")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private LinkedList<Product> productList;
	DBAccess dba;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderServlet() {
        super();
        dba = DBAccess.getInstance();
        try {
            productList = dba.getProducts();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher disp = request.getRequestDispatcher("/WEB-INF/views/Order.jsp");
        try {
        disp.forward(request, response);
        } catch (ServletException ex) {
            response.getWriter().println("A error occured: " + ex.getMessage());
        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.getServletContext().setAttribute("productList", productList);

	}

}
